﻿/**	variance.h
 *	----	Create Time Stamp	--2007-09-10 20:28:40--
 *------------------------------------------------------------------------------
 *	Mean	:
 *	Author	:Nick Shallery	(nicknide@gmail.com)
 *	Version	:1.0.3.3	(2009-01-27 16:09:34)
 *------------------------------------------------------------------------------
**/	/*YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.*/

#if			!defined(__LOLIX__SOLA__VARIANCE__H__NICK__K_IN_G__)
#define		__LOLIX__SOLA__VARIANCE__H__NICK__K_IN_G__


#if			!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)
	#include	"./lolix_cfg.h"
#endif	//	!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)


#if			LOLIX_CFG__PER_CHECK_FILE_CHUNK_SPACE
	#if			!defined(__LOLIX__LOLIX_TYPE__K_IN_G__)
		#include	"../lolix_type.h"
	#endif	//	!defined(__LOLIX__LOLIX_TYPE__K_IN_G__)
#else	//	LOLIX_CFG__PER_CHECK_FILE_CHUNK_SPACE
	#include	"../lolix_type.h"
#endif	//	LOLIX_CFG__PER_CHECK_FILE_CHUNK_SPACE


namespace	lolix{namespace	sola{
	template <typename _Ty>
	class	variance
	{
		typedef	variance<_Ty>	this_type;
	public:
		typedef	_Ty			value_type;
		typedef	size_type	size_type;

		LOLIX_INLINE_CALL	variance(void)
			: _t(_Ty())
			, _tp2(_Ty())
			, _size(0)
			{
			}

		LOLIX_INLINE_CALL	void	push_back(_Ty v)
			{
			_t += v;
			_tp2 += v*v;
			++_size;
			}

		LOLIX_INLINE_CALL	bool	empty(void)const
			{
			return _size?false:true;
			}

		LOLIX_INLINE_CALL	size_type	size(void)const
			{
			return _size;
			}

		LOLIX_INLINE_CALL	_Ty	total(void)const
			{
			return _t;
			}

		LOLIX_INLINE_CALL	_Ty	avg(void)const
			{
			return _t/_size;
			}

		LOLIX_INLINE_CALL	_Ty total_p2(void)const
			{
			return _tp2 - _t*_t/_size;
			}

		LOLIX_INLINE_CALL	_Ty avg_p2(void)const
			{
			return this->total_p2()/_size;
			}

		LOLIX_INLINE_CALL	void	clear(void)
			{
			_t = _tp2 = _Ty();
			_size = 0;
			}

		LOLIX_INLINE_CALL	this_type& operator += (const this_type& o)
			{
			_t += o._t;
			_tp2 += o._tp2;
			_size += o._size;
			return *this;
			}
	private:
		_Ty			_t;		//	total
		_Ty			_tp2;	//	total pow 2
		size_type	_size;
	};
}}


#endif	//	!defined(__LOLIX__SOLA__VARIANCE__H__NICK__K_IN_G__)

